<!DOCTYPE html>
<html lang="zh-hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/wow/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/wow/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/wow/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/wow/images/logo.svg" color="#222">

<link rel="stylesheet" href="/wow/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"single-flower.gitee.io","root":"/wow/","images":"/wow/images","scheme":"Muse","version":"8.3.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"بحث...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="Axios是什么Axios是一个基于promise的HTTP库，简单的讲就是可以发送get、post请求 Axios特性可以在浏览器中发送XMLHttpRequests可以在node.js发送http请求支持Promise API拦截请求和响应转换请求数据和响应数据能够取消请求自动转换JSON数据客户端支持保护安全免受XSRF攻击 Axios适用于什么场景浏览器发送请求，或者Node.js发送请求">
<meta property="og:type" content="article">
<meta property="og:title" content="axios封装">
<meta property="og:url" content="https://single-flower.gitee.io/wow/2021/02/10/zy/axios%E5%B0%81%E8%A3%85/index.html">
<meta property="og:site_name" content="朵朵最可爱">
<meta property="og:description" content="Axios是什么Axios是一个基于promise的HTTP库，简单的讲就是可以发送get、post请求 Axios特性可以在浏览器中发送XMLHttpRequests可以在node.js发送http请求支持Promise API拦截请求和响应转换请求数据和响应数据能够取消请求自动转换JSON数据客户端支持保护安全免受XSRF攻击 Axios适用于什么场景浏览器发送请求，或者Node.js发送请求">
<meta property="og:locale">
<meta property="article:published_time" content="2021-02-10T07:46:04.978Z">
<meta property="article:modified_time" content="2021-04-13T07:05:26.839Z">
<meta property="article:author" content="X y">
<meta property="article:tag" content="-GIT">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://single-flower.gitee.io/wow/2021/02/10/zy/axios%E5%B0%81%E8%A3%85/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-hans'
  };
</script>
<title>axios封装 | 朵朵最可爱</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="تشغيل شريط التصفح" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/wow/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">朵朵最可爱</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/wow/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-about"><a href="/wow/about/" rel="section"><i class="fa fa-user fa-fw"></i>معلومات</a></li>
        <li class="menu-item menu-item-tags"><a href="/wow/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>الوسوم</a></li>
        <li class="menu-item menu-item-categories"><a href="/wow/categories/" rel="section"><i class="fa fa-th fa-fw"></i>التصنيفات</a></li>
        <li class="menu-item menu-item-archives"><a href="/wow/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>الأرشيفات</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          المحتويات
        </li>
        <li class="sidebar-nav-overview">
          عام
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Axios%E6%98%AF%E4%BB%80%E4%B9%88"><span class="nav-number">1.</span> <span class="nav-text">Axios是什么</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Axios%E7%89%B9%E6%80%A7"><span class="nav-number">2.</span> <span class="nav-text">Axios特性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Axios%E9%80%82%E7%94%A8%E4%BA%8E%E4%BB%80%E4%B9%88%E5%9C%BA%E6%99%AF"><span class="nav-number">3.</span> <span class="nav-text">Axios适用于什么场景</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8Axios"><span class="nav-number"></span> <span class="nav-text">使用Axios</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%89%E8%A3%85Axios"><span class="nav-number">1.</span> <span class="nav-text">安装Axios</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%85%8D%E7%BD%AEaxios%E5%9F%BA%E6%9C%AC%E5%B1%9E%E6%80%A7"><span class="nav-number">2.</span> <span class="nav-text">配置axios基本属性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Axios%E6%8B%A6%E6%88%AA%E5%99%A8"><span class="nav-number">3.</span> <span class="nav-text">Axios拦截器</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9C%A8axios%E5%9F%BA%E6%9C%AC%E5%B1%9E%E6%80%A7%E4%B8%8B%E9%9D%A2%E5%86%99"><span class="nav-number">3.1.</span> <span class="nav-text">在axios基本属性下面写</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%93%8D%E5%BA%94%E6%8B%A6%E6%88%AA%E5%99%A8"><span class="nav-number">3.2.</span> <span class="nav-text">响应拦截器</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Axios%E5%B0%81%E8%A3%85"><span class="nav-number">4.</span> <span class="nav-text">Axios封装</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#get%E6%96%B9%E6%B3%95"><span class="nav-number">4.1.</span> <span class="nav-text">get方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#post%E6%96%B9%E6%B3%95"><span class="nav-number">4.2.</span> <span class="nav-text">post方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#put%E6%96%B9%E6%B3%95"><span class="nav-number">4.3.</span> <span class="nav-text">put方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#delete%E6%96%B9%E6%B3%95"><span class="nav-number">4.4.</span> <span class="nav-text">delete方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8axios%E6%96%B9%E6%B3%95"><span class="nav-number">4.5.</span> <span class="nav-text">使用axios方法:</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">X y</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/wow/archives/">
        
          <span class="site-state-item-count">23</span>
          <span class="site-state-item-name">المقالات</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">التصنيفات</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">الوسوم</span>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-hans">
    <link itemprop="mainEntityOfPage" href="https://single-flower.gitee.io/wow/2021/02/10/zy/axios%E5%B0%81%E8%A3%85/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/wow/images/avatar.gif">
      <meta itemprop="name" content="X y">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="朵朵最可爱">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          axios封装
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">نُشر في</span>

      <time title="أُنشأ: 2021-02-10 15:46:04" itemprop="dateCreated datePublished" datetime="2021-02-10T15:46:04+08:00">2021-02-10</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">عُدل في</span>
        <time title="عُدل: 2021-04-13 15:05:26" itemprop="dateModified" datetime="2021-04-13T15:05:26+08:00">2021-04-13</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">في</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/wow/categories/GIT/" itemprop="url" rel="index"><span itemprop="name">-GIT</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="Axios是什么"><a href="#Axios是什么" class="headerlink" title="Axios是什么"></a>Axios是什么</h2><p>Axios是一个基于promise的HTTP库，简单的讲就是可以发送get、post请求</p>
<h2 id="Axios特性"><a href="#Axios特性" class="headerlink" title="Axios特性"></a>Axios特性</h2><p>可以在浏览器中发送XMLHttpRequests<br>可以在node.js发送http请求<br>支持Promise API<br>拦截请求和响应<br>转换请求数据和响应数据<br>能够取消请求<br>自动转换JSON数据<br>客户端支持保护安全免受XSRF攻击</p>
<h2 id="Axios适用于什么场景"><a href="#Axios适用于什么场景" class="headerlink" title="Axios适用于什么场景"></a>Axios适用于什么场景</h2><p>浏览器发送请求，或者Node.js发送请求都可以用到Axios<br>像Vue、React、Node等项目就可以使用Axios<br>如果项目中用到了Jquery，那么就不需要用axios，因为Jquery本身就可以发送请求</p>
<h1 id="使用Axios"><a href="#使用Axios" class="headerlink" title="使用Axios"></a>使用Axios</h1><h2 id="安装Axios"><a href="#安装Axios" class="headerlink" title="安装Axios"></a>安装Axios</h2><p>npm install axios –save</p>
<h2 id="配置axios基本属性"><a href="#配置axios基本属性" class="headerlink" title="配置axios基本属性"></a>配置axios基本属性</h2><p>在src路径下新建一个文件夹，在该文件夹下的package.js中写入（是用来封装axios的，js文件名可以随便起）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> axios = <span class="built_in">require</span>(<span class="string">&#x27;axios&#x27;</span>);</span><br><span class="line">axios.defaults.baseURL = <span class="string">&#x27;http://127.0.0.1:8000&#x27;</span>;  <span class="comment">// 请求的接口主域名</span></span><br><span class="line">axios.defaults.timeout = <span class="number">10000</span>;  <span class="comment">// 请求超时设置10s</span></span><br></pre></td></tr></table></figure>
<h2 id="Axios拦截器"><a href="#Axios拦截器" class="headerlink" title="Axios拦截器"></a>Axios拦截器</h2><h3 id="在axios基本属性下面写"><a href="#在axios基本属性下面写" class="headerlink" title="在axios基本属性下面写"></a>在axios基本属性下面写</h3><p>请求拦截器</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">axios.interceptors.request.use(<span class="function"><span class="keyword">function</span> (<span class="params">config</span>) </span>&#123;</span><br><span class="line">　　<span class="comment">// 在发送请求之前做些什么</span></span><br><span class="line">　　<span class="keyword">return</span> config</span><br><span class="line">&#125;, <span class="function"><span class="keyword">function</span> (<span class="params">error</span>) </span>&#123;</span><br><span class="line">　　<span class="comment">// 对请求错误做些什么</span></span><br><span class="line"><span class="keyword">return</span> <span class="built_in">Promise</span>.reject(error)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="响应拦截器"><a href="#响应拦截器" class="headerlink" title="响应拦截器"></a>响应拦截器</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">axios.interceptors.response.use(<span class="function"><span class="keyword">function</span> (<span class="params">response</span>) </span>&#123;</span><br><span class="line">　　<span class="comment">// 对响应数据做点什么</span></span><br><span class="line">　　<span class="keyword">return</span> response</span><br><span class="line">&#125;, <span class="function"><span class="keyword">function</span> (<span class="params">error</span>) </span>&#123;</span><br><span class="line">　　<span class="comment">// 对响应错误做点什么</span></span><br><span class="line">　　<span class="keyword">return</span> <span class="built_in">Promise</span>.reject(error)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h2 id="Axios封装"><a href="#Axios封装" class="headerlink" title="Axios封装"></a>Axios封装</h2><p>axios封装常用的四种方法：get、post、put、delete<br>axios封装的好处：为了更好地维护，简化代码，易复用</p>
<h3 id="get方法"><a href="#get方法" class="headerlink" title="get方法"></a>get方法</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * get方法，对应get请求</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;String&#125;</span> </span>url [请求的url地址]</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>params [请求时携带的参数]</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">get</span>(<span class="params">url, params</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span>&#123;</span><br><span class="line">        axios.get(url, &#123;</span><br><span class="line">            params: params</span><br><span class="line">        &#125;).then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">            resolve(res.data);</span><br><span class="line">        &#125;).catch(<span class="function"><span class="params">err</span> =&gt;</span>&#123;</span><br><span class="line">            reject(err.data)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;);&#125;</span><br></pre></td></tr></table></figure>
<p>我们通过定义一个get函数，get函数有两个参数，第一个参数表示我们要请求的url地址，第二个参数是我们要携带的请求参数。</p>
<h3 id="post方法"><a href="#post方法" class="headerlink" title="post方法"></a>post方法</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * post方法，对应post请求</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;String&#125;</span> </span>url [请求的url地址]</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>params [请求时携带的参数]</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">post</span>(<span class="params">url, params</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">         axios.post(url, QS.stringify(params))</span><br><span class="line">        .then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">            resolve(res.data);</span><br><span class="line">        &#125;)</span><br><span class="line">        .catch(<span class="function"><span class="params">err</span> =&gt;</span>&#123;</span><br><span class="line">            reject(err.data)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>原理同get基本一样，但是要注意的是，post方法必须要使用对提交从参数对象进行序列化的操作</p>
<h3 id="put方法"><a href="#put方法" class="headerlink" title="put方法"></a>put方法</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * put方法，对应put请求</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;String&#125;</span> </span>url [请求的url地址]</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>params [请求时携带的参数]</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>headers [请求时的头部]</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">put</span>(<span class="params">url, params, headers</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    axios.put(url, params, headers).then(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">      resolve(res.data)</span><br><span class="line">    &#125;).catch(<span class="function">(<span class="params">err</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="comment">// debugger</span></span><br><span class="line">      reject(err.data)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>更新数据</p>
<h3 id="delete方法"><a href="#delete方法" class="headerlink" title="delete方法"></a>delete方法</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * delete方法，对应delete请求</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;String&#125;</span> </span>url [请求的url地址]</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>params [请求时携带的参数]</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>headers [请求时的头部]</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">del</span>(<span class="params">url, params, headers</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    axios.delete(url, &#123;<span class="attr">data</span>: params, headers&#125;).then(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">      resolve(res.data)</span><br><span class="line">    &#125;).catch(<span class="function">(<span class="params">err</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="comment">// debugger</span></span><br><span class="line">      reject(err.data)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>删除数据</p>
<h3 id="使用axios方法"><a href="#使用axios方法" class="headerlink" title="使用axios方法:"></a>使用axios方法:</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> axios;</span><br></pre></td></tr></table></figure>
<p>package.js里面的就写好啦，下面就要在api.js里面获取api接口了</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/wow/tags/GIT/" rel="tag"># -GIT</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/wow/2021/02/03/zy/celery%E5%BC%82%E6%AD%A5%E6%A1%86%E6%9E%B6(5.0)/" rel="prev" title="celery异步框架">
                  <i class="fa fa-chevron-left"></i> celery异步框架
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/wow/2021/02/10/zy/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/" rel="next" title="redis发布者订阅者">
                  redis发布者订阅者 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">X y</span>
</div>
  <div class="powered-by">تطبيق الموقع <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/wow/js/utils.js"></script><script src="/wow/js/motion.js"></script><script src="/wow/js/schemes/muse.js"></script><script src="/wow/js/next-boot.js"></script>

  






  





</body>
</html>
